<?php

namespace app\admin\controller;

/**
 * 后台订单控制器
 */
class Order extends Home
{
    /**
     * 会员新下订单
     */
    public function index()
    {
        /* 查询条件初始化 */
        $where = $this->getMap('create_time');
        $map = $where['map'];
        $maps = $where['maps'];
        $maps['status'] = $map['status'] = 1;
        $usernumber = input('usernumber');
        $type = input('type', 1);
        if ($usernumber) {
            switch ($type) {
                case 1:
                    $uid = Db('member')->where('usernumber', $usernumber)->value('uid');
                    break;
                case 2:
                    $uid = Db('member')->where('realname', $usernumber)->value('uid');
                    break;
            }
            $maps['uid'] = $map['uid'] = $uid;
        }
        // 记录当前列表页的cookie
//        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        self::orderList($map, $maps);

        /*追加查询条件*/
        $arr = array(
            array(
                'type' => 'input',
                'name' => 'usernumber',
                'title' => '请输入会员账号等有效信息',
                'value' => $usernumber,
                'inputid' => '',
                'readonly' => '',
            ),
            array(
                'type' => 'select',
                'name' => 'type',
                'option' => array(
                    '1' => '会员账号',
                    '2' => '会员姓名'
                ),
                'value' => $type,
                'inputid' => '',
                'readonly' => '',
            ),

        );
        $this->searchCondition($maps, $arr);
        $this->assign('action', 'index');
        $this->assign('search', '/admin/Order/orderIndexd');
        $this->assign('status', 1);
        $this->assign('title', '新下订单');
        return $this->fetch('order');
    }

    /**
     * 已发货订单
     */
    public function deliveryOrder()
    {
        /* 查询条件初始化 */
        $where = $this->getMap('create_time');
        $map = $where['map'];
        $maps = $where['maps'];

        $maps['status'] = $map['status'] = 10;

        $usernumber = input('usernumber');
        $type = input('type', 1);
        if ($usernumber) {
            switch ($type) {
                case 1:
                    $uid = Db('member')->where('usernumber', $usernumber)->value('uid');
                    break;
                case 2:
                    $uid = Db('member')->where('realname', $usernumber)->value('uid');
                    break;
            }
            $maps['uid'] = $map['uid'] = $uid;
        }

        // 记录当前列表页的cookie
//        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->orderList($map, $maps);
        $arr = array(
            array(
                'type' => 'input',
                'name' => 'usernumber',
                'title' => '请输入会员账号等有效信息',
                'value' => $usernumber,
                'inputid' => '',
                'readonly' => '',
            ),
            array(
                'type' => 'select',
                'name' => 'type',
                'option' => array(
                    '1' => '会员账号',
                    '2' => '会员姓名'
                ),
                'value' => $type,
                'inputid' => '',
                'readonly' => '',
            ),
        );
        $this->searchCondition($maps, $arr);
        $this->assign('action', 'deliveryOrder');
        $this->assign('title', '已发货订单');
        return $this->fetch('order');
    }

    public function orderIndexd()
    {
        $where = $this->getMap('create_time');
        $map = $where['map'];
        $map['status'] = 1;
        $mid = is_admin_login();
        if ($mid != 1) {
            $map['mid'] = $mid;
        }

        $list = Db('shoplist')->where($map)->order('id desc')->select();
        foreach ($list as &$vv) {
            $addressid = Db('shop_order')->where(array('id' => $vv['orderid']))->value("addressid");
            $vv['paytype'] = Db('shop_order')->where(array('id' => $vv['orderid']))->value("paytype");
            $address = Db('transport')->where(array('id' => $addressid))->find();
            $area = $this->getAddress($address['area']);
            $addresss = $area . $address['address'];
            $vv['name'] = $address['realname'];
            $vv['mobile'] = $address['mobile'];
            $vv['address'] = $addresss;
        }

        $title = '待发货订单';
        $a = array(
            array('orderid', '订单号'),
            array('usernumber', '会员账号'),
            array('realname', '会员姓名'),
            array('goodname', '商品名称'),
            array('num', '商品数量'),
            array('ispay', '支付方式'),
            array('name', '收货人'),
            array('mobile', '联系方式'),
            array('address', '收货地址'),
            array('createtime', '时间'),
        );

        foreach ($list as $k => &$v) {
            $v['orderid'] = $v['tag'];
            $v['usernumber'] = Db('member')->where(array('uid' => $v['uid']))->value('usernumber');
            $v['realname'] = Db('member')->where(array('uid' => $v['uid']))->value('realname');
            switch ($v['paytype']) {
                case 1:
                    $v['ispay'] = HASBILL;
                    break;
                case 2:
                    $v['ispay'] = HASMONEY . '+' . HASCP;
                    break;
                case 3:
                    $v['ispay'] = HASBILL . '+' . HASCP;
                    break;
            }

            $v['createtime'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        exportExcel($title, $a, $list);
    }

    public function importShop2()
    {

        /* 查询条件初始化 */
        $map = array('status' => 2);
        // 记录当前列表页的cookie
        $list = M('shop_order')->where($map)->select();
        $title = '已发货订单';
        $a = array(
            array('orderid', '订单号'),
            array('nickname', '会员昵称'),
            array('usernumber', '会员账号'),
            array('total', '金额'),
            array('ispay', '支付方式'),
            array('createtime', '时间'),
        );
        foreach ($list as $k => &$v) {
            $v['orderid'] = $v['orderid'];
            $v['nickname'] = M('member')->where(array('uid' => $v['uid']))->getField('nickname');
            $v['usernumber'] = M('member')->where(array('uid' => $v['uid']))->getField('usernumber');
            $v['total'] = $v['total'];
            $v['ispay'] = '购物币';
            $v['createtime'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $res = exportExcel($title, $a, $list);

    }

    public function importShop3()
    {

        /* 查询条件初始化 */
        $map = array('status' => 3);
        // 记录当前列表页的cookie
        $list = M('shop_order')->where($map)->select();
        $title = '已签收订单';
        $a = array(
            array('orderid', '订单号'),
            array('nickname', '会员昵称'),
            array('usernumber', '会员账号'),
            array('total', '金额'),
            array('ispay', '支付方式'),
            array('createtime', '时间'),
        );
        foreach ($list as $k => &$v) {
            $v['orderid'] = $v['orderid'];
            $v['nickname'] = M('member')->where(array('uid' => $v['uid']))->getField('nickname');
            $v['usernumber'] = M('member')->where(array('uid' => $v['uid']))->getField('usernumber');
            $v['total'] = $v['total'];
            $v['ispay'] = '购物币';
            $v['createtime'] = date('Y-m-d H:i:s', $v['create_time']);
        }
        $res = exportExcel($title, $a, $list);

    }

    /**
     * 已签收订单
     */
    public function signforOrder()
    {
        /* 查询条件初始化 */
        $where = $this->getMap('create_time');
        $map = $where['map'];
        $maps = $where['maps'];
        $maps['status'] = $map['status'] = 3;
        $usernumber = input('usernumber');
        $type = input('type', 1);
        if ($usernumber) {
            switch ($type) {
                case 1:
                    $uid = Db('member')->where('usernumber', $usernumber)->value('uid');
                    break;
                case 2:
                    $uid = Db('member')->where('realname', $usernumber)->value('uid');
                    break;
            }
            $maps['uid'] = $map['uid'] = $uid;
        }
        // 记录当前列表页的cookie
//        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $this->orderList($map, $maps);
        $arr = array(
            array(
                'type' => 'input',
                'name' => 'usernumber',
                'title' => '请输入会员账号等有效信息',
                'value' => $usernumber,
                'inputid' => '',
                'readonly' => '',
            ),
            array(
                'type' => 'select',
                'name' => 'type',
                'option' => array(
                    '1' => '会员账号',
                    '2' => '会员姓名'
                ),
                'value' => $type,
                'inputid' => '',
                'readonly' => '',
            ),
        );
        $this->searchCondition($maps, $arr);
        $this->assign('action', 'signforOrder');
        $this->assign('title', '已签收订单');
        return $this->fetch('order');
    }

    /**
     * 订单列表
     * @param $map
     * @param $maps
     */
    public function orderList($map, $maps)
    {
        $list = Db('shoplist')->where($map)->order('id desc')->paginate(20);
        $page = $list->render();

        foreach ($list as $v) {
//            $shop = Db('shop')->field('sid,category_id')->where('id', $v['goodid'])->find();
//            $v['cate_name'] = Db('shop_category')->where('id', $shop['category_id'])->value('title');
        }

        $this->assign('status', $map['status']);
        $this->assign('_list', $list);
        $this->assign('page', $page);
    }

    /**
     * 订单详情
     */
    public function details($id = 0)
    {
        $action = input('action', 'index');
        /* 获取数据 */
        $list = Db('shoplist')->where("id", $id)->find();
        if (!$list) {
            $this->error('非法操作！');
        }
        $info = Db('shop_order')->where(array('id' => $list['orderid']))->find();
        $addressid = $info["addressid"];
        $address = Db('transport')->where(array('id' => $addressid))->find();
        if ($address) {
            $address['area'] = $this->getAddress($address['area']);
            $address['address'] = $address['area'] . $address['address'];
        }

        $this->assign('address', $address);
        $this->assign('list', $list);
        $this->assign('info', $info);
        $this->assign('action', $action);
        $this->assign('title', '订单详情');
        return $this->fetch();
    }

    /**
     * 处理投诉
     * @return mixed
     */
    public function handleReply()
    {
        $map['id'] = input('id');
        $list = Db('shoplist')->where($map)->find();
        if (!in_array($list['status'], [2, 5])) {
            $this->error('该订单不能处理！');
        }
        if (request()->isPost()) {
            $data = input('post.');
            $res = Db('shoplist')->where('id', $data['id'])->update($data);
            if ($res) {
                $this->success('处理成功！');
            } else {
                $this->error('处理失败！');
            }
        }
        $list['picture'] = get_cover($list['replyid'], 'path');
        $this->assign('list', $list);
        $this->assign('title', '处理投诉');
        return $this->fetch('handlereply');
    }

    public function changeInfo()
    {
        if (request()->isPost()) {
            $info = input('info');
            $id = input('id');
            $order = Db('shop_order')->where('id', $id)->find();
            if (!$order) {
                $this->error('该订单不存在！');
            }
            $res = Db('shop_order')->where('id', $id)->setField('info', $info);
            if ($res) {
                $this->success('更新成功！');
            } else {
                $this->error('更新失败！');
            }
        } else {
            $this->error('非法操作！');
        }
    }

    /**
     * 更换发货会员
     */
    public function changeTid()
    {
        if (request()->isPost()) {
            $id = input('id');
            $order = Db('shop_order')->where('id', $id)->find();
            if (!$order || $order['status'] != 1) {
                $this->error('该订单不可更换发货会员！');
            }
            $mid = is_admin_login();
            if ($mid != 1) {
                $this->error('普通管理员无权修改发货会员！');
            }
            $tuid = input('tuid');
            if ($order['tuijianid'] == $tuid) {
                $this->error('数据未变！');
            }

            $res = Db('shop_order')->where('id', $id)->setField('tuijianid', $tuid);
            if ($res) {
                $this->success('更新成功！');
            } else {
                $this->error('更新失败！');
            }
        } else {
            $this->error('非法操作！');
        }
    }

    /**
     * 更新快递单号
     */
    public function changeToolid()
    {
        if (request()->isPost()) {
            $toolid = input('toolid');
            $id = input('id');
            $shoplist = Db('shoplist')->where('id', $id)->find();
            if (!$shoplist) {
                $this->error('该订单不存在！');
            }
            if ($shoplist['status'] != 1) {
                $this->error('该订单不可发货！');
            }
            $res = Db('shoplist')->where('id', $id)->setField('toolid', $toolid);
            if ($res) {
                $this->success('更新成功！');
            } else {
                $this->error('更新失败！');
            }
        } else {
            $this->error('非法操作！');
        }
    }

    /**
     * 订单发货
     */
    public function send()
    {
        $id = input('id');
        $list = Db('shoplist')->where('id', $id)->find();
        if ($list['status'] != 1) {
            $this->error('该订单不可发货！');
        }
        if (request()->isPost()) {
            $status = 10;
            $data['tool'] = input('tool');
            $data['send_name'] = input('send_name');
            $data['send_contact'] = input('send_contact');
            $data['send_address'] = input('send_address');
            $data['send_time'] = time();
            $data['status'] = $status;
            $toolid = input('toolid');
            if ($toolid) {
                $data['toolid'] = $toolid;
            }

            $result = Db('shoplist')->where(array('id' => $id))->update($data);
            if ($result) {
                Db('shoplist')->where('id', $id)->update(['status' => $status, 'send_time' => time()]);
                $num = Db('shoplist')->where('orderid', $list['orderid'])->where('status', 'lt', $status)->count();
                if ($num == 0) {
                    Db('shop_order')->where('id', $list['orderid'])->update(['status' => $status, 'send_time' => time()]);
                }
                $this->success('发货成功', url('index'));
            } else {
                $this->error('发货失败');
            }
        } else {
            $this->error('非法操作！');
        }
    }

    /**
     * 删除订单
     */
    public function del()
    {
        if (IS_POST) {
            $ids = I('post.id');

            $order = M("shop_order");
            if (is_array($ids)) {
                foreach ($ids as $id) {
                    $order->where("id='$id'")->delete();
                    $shop = M("shoplist");
                    $shop->where("orderid='$id'")->delete();
                }
            }
            $this->success("删除成功！");
        } else {
            $id = I('get.id');
            $db = M("shop_order");
            $status = $db->where(array('id' => $id))->delete();
            if ($status) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }
    }
}